<?php
namespace app\api\command;

use app\api\controller\Base;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use wheat\Wheat;

class RoomCheck extends Command
{
    protected function configure()
    {
        $this->setName('roomcheck')->setDescription('check room user online');
    }

    protected function execute(Input $input, Output $output)
    {
        /* 永不超时 */
        ini_set('max_execution_time', 0);
        $this->doCron();
    }


    public function doCron(){

        $cache = cache('command'.__CLASS__);
        if ($cache){
            echo 'running<br />';return false;
        }else{
            cache('command'.__CLASS__,true,1800);
        }

        $map['status'] = 1;
        Db::name('room')->where($map)->chunk(50,function ($items){
            foreach ($items as $k => $v){
                $wheat = Wheat::wheat($v['room_id']);
                if ($wheat){
                    $refresh = 0;
                    foreach ($wheat as $key => $val){
                        if ($key == 0){
                            if ($v['type'] == 2 || $v['type'] == 4){
                                //娱乐房和聊天房房主永不下麦
                                continue;
                            }
                        }

                        if (!is_zero($val['user_id'])){
                            $online = checkOnline($val['user_id']);
                            if ($online == 0){
                                $refresh++;
                                $wheat[$key]['user_id']    = "0";
                                $wheat[$key]['header_img'] = "";
                                $wheat[$key]['nick_name']  = "";
                                $wheat[$key]['power']      = 4;
                            }
                        }
                        if ($refresh != 0){
                            //刷新麦序
                            Wheat::setWheat($v['room_id'],$wheat);
                            Base::refreshWheat($v['room_id']);
                        }

                    }
                }
            }
        });

        cache('command'.__CLASS__,null);

    }
}